# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================



# ==================================
# Calculating mean party-level dispersion
# (Figure 8 in Online Appendix)

data <- read.csv("dispersion_in_parties.csv")
av.ipv <- tapply(data[,3], data[,1], mean)
av.ipv

se <- function(x) sqrt(var(x)/length(x))
se.ipv <- tapply(data[,3], data[,1], se)

conf.1986.upp <- av.ipv[1]+(1.96*(se.ipv[1]))
conf.1986.low <- av.ipv[1]-(1.96*(se.ipv[1]))

conf.1990.upp <- av.ipv[2]+(1.96*(se.ipv[2]))
conf.1990.low <- av.ipv[2]-(1.96*(se.ipv[2]))

conf.1993.upp <- av.ipv[3]+(1.96*(se.ipv[3]))
conf.1993.low <- av.ipv[3]-(1.96*(se.ipv[3]))

conf.1996.upp <- av.ipv[4]+(1.96*(se.ipv[4]))
conf.1996.low <- av.ipv[4]-(1.96*(se.ipv[4]))

conf.2000.upp <- av.ipv[5]+(1.96*(se.ipv[5]))
conf.2000.low <- av.ipv[5]-(1.96*(se.ipv[5]))

conf.2003.upp <- av.ipv[6]+(1.96*(se.ipv[6]))
conf.2003.low <- av.ipv[6]-(1.96*(se.ipv[6]))

conf.2005.upp <- av.ipv[7]+(1.96*(se.ipv[7]))
conf.2005.low <- av.ipv[7]-(1.96*(se.ipv[7]))

conf.2009.upp <- av.ipv[8]+(1.96*(se.ipv[8]))
conf.2009.low <- av.ipv[8]-(1.96*(se.ipv[8]))

conf.1986.low
conf.1986.upp

conf.1990.low
conf.1990.upp

conf.1993.low
conf.1993.upp

conf.1996.low
conf.1996.upp

conf.2000.low
conf.2000.upp

conf.2003.low
conf.2003.upp

conf.2005.low
conf.2005.upp

conf.2009.low
conf.2009.upp

years <- c(1986, 1990, 1993, 1996, 2000, 2003, 2005, 2009)

par(mfrow = c(1, 1), mar = c(4,4,2,1), tcl = -0.25, mgp = c(1.75, 0.6, 0),
    font.main = 1, cex.main = 2)

plot(years, av.ipv, pch=19, ylim=c(0,1.05), 
     ylab="Mean Party-Level Dispersion", xlab="Year", xaxt="n", cex.lab=1.5)
axis(1, at=c(1986, 1990, 1993, 1996, 2000, 2003, 2005, 2009), cex.axis=1.5)
lines(years, av.ipv, lwd =2)
arrows(1986, conf.1986.upp, 1986, conf.1986.low, code = 3, lwd=1, angle=90)
arrows(1990, conf.1990.upp, 1990, conf.1990.low, code = 3, lwd=1, angle=90)
arrows(1993, conf.1993.upp, 1993, conf.1993.low, code = 3, lwd=1, angle=90)
arrows(1996, conf.1996.upp, 1996, conf.1996.low, code = 3, lwd=1, angle=90)
arrows(2000, conf.2000.upp, 2000, conf.2000.low, code = 3, lwd=1, angle=90)
arrows(2003, conf.2003.upp, 2003, conf.2003.low, code = 3, lwd=1, angle=90)
arrows(2005, conf.2005.upp, 2005, conf.2005.low, code = 3, lwd=1, angle=90)
arrows(2009, conf.2009.upp, 2009, conf.2009.low, code = 3, lwd=1, angle=90)

arrows(1996, 0.8, 1996, 0.3, lwd=3, col = "black")
text(x=2000.8, y=0.9, labels="First Election Under MMM", cex=2, xpd=NA, pos=2, col = "black")

rm(list=ls())









# ==================================
# Differences-in-means tests reported on page 16 of Appendix

data <- read.csv("dispersion_in_parties.csv")

data.pre <- data[data$year<1996,]
data.post <- data[data$year>1993,]
nrow(data.pre)
nrow(data.post)
t.test(data.pre$var, data.post$var)

data.93 <- data[data$year==1993,]
data.96 <- data[data$year==1996,]
nrow(data.93)
nrow(data.96)
t.test(data.93$var, data.96$var)

data.93 <- data[data$year==1993,]
data.09 <- data[data$year==2009,]
t.test(data.93$var, data.09$var)


rm(list=ls())
